﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KEEPER_BRR_13dotNET.BusinessLogic;
using KP_CommonLibrary.Logs;
using KP_CommonLibrary;
using KP_CommonLibrary.KP_CommonControls;
using KP_CommonLibrary.KP_Interface;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;
using KP_CommonLibrary.KP_Function;
using DevComponents.DotNetBar.SuperGrid;

namespace KEEPER_BRR_13dotNET.Presentation
{
    public partial class Ucl_DsHoSo1Cua : KP_CommonLibrary.KP_CommonControls.KP_UserControl, IRunCommand
    {
        BL_KPHoSo blHoSo;
        public static Ucl_DsHoSo1Cua Current;
        public int Ma_Tt1 = -1;
        public int Ma_Tt2 = -1;
        public string MaVTri = "";
        public string MaVTriNhan = "";
        string viTriHt = "Một cửa";
        string viTriNhan = "Phòng thẩm định";
        public Ucl_DsHoSo1Cua()
        {
            InitializeComponent();
            if (Current == null || Current.IsDisposed)
                Current = this;
            if (!DesignMode)
                Disposed += new EventHandler(Ucl_DsHoSo1Cua_Disposed);
        }

        void Ucl_DsHoSo1Cua_Disposed(object sender, EventArgs e)
        {
            Current = null;
        }
        public override bool DispatchCmdKey(ref Message msg, Keys keyData)
        {
            return base.DispatchCmdKey(ref msg, keyData);
        }

        private void Ucl_DsHoSo1Cua_Load(object sender, EventArgs e)
        {

            try
            {

                blHoSo = new BL_KPHoSo();
               // blHoSo.InitDependencyBussines();
                MainGrid.PrimaryGrid.DataSource = blHoSo.FindDangKyKdByKeyword(txtGiaTriCanTim.Text, Ma_Tt1, Ma_Tt2, MaVTri);

            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Lấy dữ liệu", ex);
                KPMessageBox.Show("Lấy dữ liệu:" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
        }

        private void txtGiaTriCanTim_ButtonCustomClick(object sender, EventArgs e)
        {
            Ucl_DsHoSo1Cua_Load(sender, e);

        }

        private void MainGrid_RowDoubleClick(object sender, DevComponents.DotNetBar.SuperGrid.GridRowDoubleClickEventArgs e)
        {
            DataRow row = null;
            FrmE_HoSoMoi frm = null;
            try
            {
                row = MainGrid.GetCurrentRow();
                frm = new FrmE_HoSoMoi();
                // frm.ViewDetail(row,false,null,null);
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Xem thông tin hồ sơ", ex);
                KPMessageBox.Show("Xem thông tin hồ sơ:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
            finally
            {
                if (frm != null)
                    frm.Dispose();
            }

        }

        private void biThemMoi_Click(object sender, EventArgs e)
        {
            FrmE_HoSoMoi frm = null;
            try
            {
                frm = new FrmE_HoSoMoi();
                frm.AddNew(null);
                frm.Invalidate();
                frm.ShowDialog();
                //frm.RunCommand(this, "DK");
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Thêm mới hồ sơ", ex);
                KPMessageBox.Show("Thêm mới hồ sơ:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
            finally
            {
                if (frm != null)
                    frm.Dispose();
            }
        }

        private void biCapLai_Click(object sender, EventArgs e)
        {
            Frm_InputSoGpKd frm = null;
            Frm_NhanHoSo2 frm2 = null;
            try
            {
                //frm = new Frm_InputSoGpKd();
                //frm.CanAccept = new CanAccept(KiemTraTruocKhiCapLai);
                //DialogResult result = frm.ShowDialog();

                //if (result == DialogResult.OK)
                //{
                frm2 = new Frm_NhanHoSo2();
                frm2.ShowDialog();
                //frm2.CapLaiGPKD(frm.Ma_Hs);

                //  }
                // frm.RunCommand(this, "DK");
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Cấp lại giấy phép kinh doanh", ex);
                KPMessageBox.Show("Cấp lại giấy phép kinh doanh:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
            finally
            {
                if (frm != null)
                    frm.Dispose();
                if (frm2 != null)
                    frm2.Dispose();
            }
        }

        private void biThayDoi_Click(object sender, EventArgs e)
        {
            Frm_InputSoGpKd frm = null;
            FrmE_HoSoMoi frm2 = null;
            try
            {
                frm = new Frm_InputSoGpKd();
                DialogResult result = frm.ShowDialog();
                if (result == DialogResult.OK)
                {
                    frm2 = new FrmE_HoSoMoi();
                    // frm2.ThayDoiNdDkKd(frm.Ma_Hs);

                }
                // frm.RunCommand(this, "DK");
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Thay đổi hồ sơ", ex);
                KPMessageBox.Show("Thay đổi hồ sơ:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
            finally
            {
                if (frm != null)
                    frm.Dispose();
                if (frm2 != null)
                    frm2.Dispose();
            }
        }
        public bool KiemTraTruocKhiCapLai(string soGp)
        {
            string message;
            bool temp = blHoSo.KiemTraTruocKhiCapLai(soGp, out message);
            if (temp)
                return true;
            else
            {
                KPMessageBox.Show(message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Warning);
                return false;
            }

            // return true;
        }
        #region IRunCommand Members

        public void RunCommand(object sender, object commandData)
        {

            string _params = (commandData as DataRow)["Params"].ToString();
            Dictionary<string, string> allParams = ArgsParser.Parse1(_params);
            string value = "";
            if (allParams.TryGetValue("TrangThai1", out value))
                Ma_Tt1 = int.Parse(value);
            else
                Ma_Tt1 = -1;

            if (allParams.TryGetValue("TrangThai2", out value))
                Ma_Tt2 = int.Parse(value);
            else
                Ma_Tt2 = -1;
            if (allParams.TryGetValue("ViTri", out value))
                MaVTri = value;
            else
                MaVTri = CauHinh.MaViTri1();
            if (allParams.TryGetValue("ViTriNhan", out value))
                MaVTriNhan = value;
            else
                MaVTriNhan = CauHinh.MaViTri1();


            BL_KPDmVTri blViTri = new BL_KPDmVTri();

            viTriNhan = blViTri.GetTenViTri(MaVTri);
            viTriHt = blViTri.GetTenViTri(MaVTriNhan);

            string title = "";
            if (sender is ButtonItem)
                title = (sender as ButtonItem).Text;
            if (sender is Node)
                title = (sender as Node).Text;
            AppContext.MainForm.ShowControlInTab(this, title);

        }

        public void RunCommand(object sender, string menuId, string commandName)
        {


        }

        #endregion

        private void MainGrid_PreRenderRow(object sender, DevComponents.DotNetBar.SuperGrid.GridPreRenderRowEventArgs e)
        {
            DataRow row = MainGrid.GetDataRow(e.GridRow.RowIndex);
            if (row != null)
            {
                if (row["Ma_Tt"].ToString() == Ma_Tt1.ToString())
                {
                    e.GridRow.CellStyles.Default.Background.Color1 = Properties.Settings.Default.NormalColor;
                    e.GridRow.CellStyles.Selected.Background.Color1 = Properties.Settings.Default.NormalColor;
                    e.GridRow.CellStyles.Selected.Background.Color2 = Color.GreenYellow;

                }
                if (row["Ma_Tt"].ToString() == Ma_Tt2.ToString())
                {
                    e.GridRow.CellStyles.Default.Background.Color1 = Properties.Settings.Default.CancelColor; ;
                    e.GridRow.CellStyles.Selected.Background.Color1 = Properties.Settings.Default.CancelColor;
                }



            }
        }

        private void biSuaHS_Click(object sender, EventArgs e)
        {
            FrmE_HoSoMoi frm = null;
            try
            {

                DataRow row = MainGrid.GetCurrentRow();
                if (row != null)
                {
                    frm = new FrmE_HoSoMoi();
                    // frm.EditHoSo(row["Stt_Rec"].ToString());
                    //frm.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Sửa hồ sơ", ex);
                KPMessageBox.Show("Sửa hồ sơ:" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);
            }
            finally
            {
                if (frm != null)
                    frm.Dispose();
            }
        }

        private void biTamNgung_Click(object sender, EventArgs e)
        {
            Frm_InputSoGpKd frm = null;
            FrmE_HoSoMoi frm2 = null;
            try
            {
                frm = new Frm_InputSoGpKd();
                frm.CanAccept = new CanAccept(KiemTraTruocKhiCapLai);
                DialogResult result = frm.ShowDialog();
                if (result == DialogResult.OK)
                {
                    frm2 = new FrmE_HoSoMoi();
                    // frm2.TamNgungGPKD(frm.Ma_Hs);

                }
                // frm.RunCommand(this, "DK");
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Tạm ngừng hoạt động kinh doanh", ex);
                KPMessageBox.Show("Tạm ngừng hoạt động kinh doanh:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
            finally
            {
                if (frm != null)
                    frm.Dispose();
                if (frm2 != null)
                    frm2.Dispose();
            }
        }

        private void biChamDut_Click(object sender, EventArgs e)
        {
            Frm_InputSoGpKd frm = null;
            FrmE_HoSoMoi frm2 = null;
            try
            {
                frm = new Frm_InputSoGpKd();
                frm.CanAccept = new CanAccept(KiemTraTruocKhiCapLai);
                DialogResult result = frm.ShowDialog();
                if (result == DialogResult.OK)
                {
                    frm2 = new FrmE_HoSoMoi();
                    // frm2.ChamDutHDGPKD(frm.Ma_Hs);

                }
                // frm.RunCommand(this, "DK");
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Fatal("Tạm ngừng hoạt động kinh doanh", ex);
                KPMessageBox.Show("Tạm ngừng hoạt động kinh doanh:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
            finally
            {
                if (frm != null)
                    frm.Dispose();
                if (frm2 != null)
                    frm2.Dispose();
            }
        }

        private void biChuyenHoSo_Click(object sender, EventArgs e)
        {
            List<GridRow> selectedRow = new List<GridRow>();
            for (int i = 0; i < MainGrid.PrimaryGrid.Rows.Count; i++)
            {
                GridRow row = MainGrid.PrimaryGrid.Rows[i] as GridRow;
                if (row.Checked)
                    selectedRow.Add(row);
            }
            if (selectedRow.Count == 0)
            {
                KPMessageBox.Show("Chưa chọn hồ sơ cần chuyển", KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Warning);
                return;
            }
            Frm_LanChuyenEdit frm = new Frm_LanChuyenEdit();
            frm.NoiDung = string.Format("Chuyển {0} hồ sơ từ \"{1}\" sang \"{2}\"", selectedRow.Count, viTriHt, viTriNhan);
            DialogResult result = frm.ShowDialog();
            string noidung = frm.NoiDung;
            bool print = frm.InPhieuChuyen;
            frm.Dispose();
            if (result == DialogResult.OK)
            {
                try
                {
                    BL_KPChuyenHSo blChuyenHs = new BL_KPChuyenHSo();
                    string maChuyen = blChuyenHs.Insert(noidung, MaVTri, MaVTriNhan, selectedRow);
                    if (print)
                    {
                        //In phieu
                    }
                    KPMessageBox.Show("Đã chuyển thành công " + selectedRow.Count.ToString() + " hồ sơ");
                    foreach (GridRow node in selectedRow)
                        MainGrid.PrimaryGrid.Rows.Remove(node);
                }
                catch (Exception ex)
                {
                    LogUtil.Logger.Fatal("Chuyển hồ sơ", ex);
                    KPMessageBox.Show("Gặp lỗi khi chuyển hồ sơ:\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

                }
            }



        }

    }
}
